#include "liste.h"

#include <stdlib.h>

Liste* initListe() {
	Liste* tmp = calloc(sizeof(Liste), 1);
	tmp->next = NULL;
	tmp->punkt = NULL;
	return tmp;
}

Punkt* getPunkt(const Liste* _list) {
	return ((Liste*)_list)->punkt;
}

Punkt* getElem(const Liste* _list, int _elem) {
	int i;
	Liste* runner = (Liste*)_list;
	for (i=0; i<_elem; ++i) {
		runner = getNext(runner);
	}
	return getPunkt(runner);
}

Liste* getNext(const Liste* _list) {
	return _list->next;
}

Liste* addPunkt(Liste* _list, Punkt* _point) {
	Liste * runner = _list;
	while (getNext(runner))
		runner = getNext(runner);
	if (!getPunkt(runner))
		runner->punkt = _point;
	else {
		runner->next = calloc(sizeof(Liste), 1);
		runner->next->next = NULL;
		runner->next->punkt = _point;
	}
	return _list;
}

Liste* addPunktxy(Liste* _list, const double _x, const double _y) {
	Punkt* tmp = calloc(sizeof(Liste), 1);
	tmp->x = _x;
	tmp->y = _y;
	return addPunkt(_list, tmp);
}

unsigned int getCount(const Liste* _list) {
	unsigned int count = 0;
	Liste* runner = (Liste*) _list;
	while(runner) {
		++count;
		runner = getNext(runner);
	}
	return count;
}
